草庐IT

javascript var 与这个

全部标签

c++ - 不使用这个的 constexpr 成员函数?

请考虑以下两个C++14程序:程序1:structS{constexprintf()const{return42;}};Ss;intmain(){constexprintx=s.f();returnx;}程序2:structS{constexprintf()const{return42;}};intg(Ss){constexprintx=s.f();returnx;}intmain(){Ss;returng(s);}这些程序中的一个或两个都不是良构的吗?为什么/为什么不? 最佳答案 这两个程序都是良构的。C++14标准要求s.f()

c++ - 如何纠正这个常量?

我有一个似乎无法解决的常量正确性问题。这是我的程序的结构:classNode{private:intid;std::setneighbours;public:Node();Node(intid_p);voidset_id(constint&id_p);intget_id()const;voidadd_neighbour(Node*neighbour);boolis_neighbour(Node*neighbour)const;friendbooloperatornode_list;public:Graph();voidadd_node(intid);constNode*get_node

c++ - 为什么这个 C++ 成员函数没有被编译器用 -O3 优化?

下面声明的C++vector类中的norm成员函数被标记为const并且(据我所知)没有包含任何副作用。templatestructvector{doublev[N];doublenorm()const{doubleret=0;for(inti=0;i&x){returnx.norm()+x.norm();}如果我在vector的const实例化上多次调用norm(参见上面的test函数)gcc编译器(版本5.4)和优化打开(即-O3)然后编译器内联norm,但仍然计算norm的结果多次,即使结果不应该改变。为什么编译器不优化对norm的第二次调用而只计算一次这个结果?Thisansw

c++ - 为什么这个程序没有优化掉?

考虑以下简单程序(改编自thisquestion):#includeintmain(intargc,char**argv){intmul1[10]={4,1,8,6,3,2,5,8,6,7};//sum=50intmul2[10]={4,1,8,6,7,9,5,1,2,3};//sum=46intx1=std::atoi(argv[1]);intx2=std::atoi(argv[2]);intresult=0;//Foreachelementinmul1/mul2,accumulatetheproductwithx1/x2inresultfor(inti=0;i我相信它在功能上等同于

c++ - 为什么 const 在这个模板结构中丢失了?

已知以下函数指针有不同的类型:voidfoo_int_ref(int&);voidfoo_const_int_ref(constint&);static_assert(!std::is_same::value,"Typesshouldbedifferent");让我们考虑这个概括:templatevoidfoo(Tt){}templatestructref_vs_const_ref{typedefdecltype(foo)foo_T;typedefdecltype(foo)foo_const_T;};usingint_ref_vs_const_ref=ref_vs_const_ref;

c++ - 为什么 ICC 以这种方式展开这个循环并使用 lea 进行算术运算?

查看ICC17生成的用于迭代std::unordered_map的代码(使用https://godbolt.org)让我很困惑。我将示例提炼为:longcount(void**x){longi=0;while(*x){++i;x=(void**)*x;}returni;}使用ICC17编译它,使用-O3标志,导致以下反汇编:count(void**):xoreax,eax#6.10movrcx,QWORDPTR[rdi]#7.11testrcx,rcx#7.11je..B1.6#Prob1%#7.11movrdx,rax#7.3..B1.3:#Preds..B1.4..B1.2incr

c++ - 这个模板解析冲突叫什么?

我在使用g++7.3编译这个最小示例时遇到问题templatestructconflict{};templatestructs{intconflict;};templateboolgo(){s*sp;returnsp->conflict实际的错误信息并没有透露什么:||test.cpp:Infunction‘boolgo()’:test.cpp|16col24|error:type/valuemismatchatargument1intemplateparameterlistfor‘templatestructconflict’||returnsp->conflict事实上,编译器试图

c++ - 为什么 GCC 不能向量化这个函数和循环?

我正在尝试使函数启用SIMD,并通过函数调用对循环进行矢量化。#include#pragmaompdeclaresimddoubleBlackBoxFunction(constdoublex){return1.0/sqrt(x);}doubleComputeIntegral(constintn,constdoublea,constdoubleb){constdoubledx=(b-a)/n;doubleI=0.0;#pragmaompsimdreduction(+:I)for(inti=0;i对于上面的代码,如果我用icpc编译:icpcworker.cc-qopenmp-qopt-r

c++ - 这个单例实现有问题吗?

我通常习惯于以这种方式实现单例模式,因为它非常简单:classMyClass{public:MyClass*GetInstance(){staticMyClassinstance;return&instance;}private://Disallowcopyconstruction,copyassignment,andexternal//defaultconstruction.};这似乎比创建静态实例指针、在源文件中对其进行初始化,以及在带有守卫的实例函数中使用动态内存分配要容易得多。是否有我没有看到的缺点?它对我来说看起来是线程安全的,因为我认为第一个到达第一行的线程会导致实例化——

c++ - 请解释这个执行强制转换和类型检查的硬核宏

以下代码来自必须同时使用C和C++编译的现有应用程序。有一个宏:/*Type-checkingmacrotoprovideargumentsforCoCreateInstance()etc.*Thepointerarithmeticisacompile-timepointertypecheckthat'obj'*reallyisa'type**',butisintendedtohavenoeffectatruntime.*/#defineCOMPTR(type,obj)&IID_##type,\(void**)(void*)((obj)+(sizeof((obj)-(type**)(o